package com.sdp.wz.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wz.cache.WzCleanCache;
import com.sdp.wz.db.wz_type.entity.WzType;
import com.sdp.wz.db.wz_type.service.IWzTypeService;
import com.sdp.wz.utils.WzConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/WzTypeController")
public class WzTypeController {

	@Autowired
	private Query query;

	@Autowired
	private IWzTypeService iWzTypeService;
	
	@Autowired
	private WzCleanCache wzCleanCache;
	
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/queryWzTypeTableTree")
	public Result<?> queryWzTypeTableTree() {
		return Result.ok(iWzTypeService.queryWzTypeTableTree());
	}

	@PostMapping(value = "/queryWzTypeTreeSelect")
	public Result<?> queryWzTypeTreeSelect() {
		return Result.ok(iWzTypeService.queryWzTypeTreeSelect());
	}
	
	@GetMapping(value = "/queryTypeLevelCountByNo")
	public Result<?> page(@RequestParam(name="typeNo",required=true) String typeNo) {
		return Result.ok(iWzTypeService.queryTypeLevelCountByNo(typeNo));
	}
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWzTypeService, WzType.class , dataCenter));
	}
	
	@PostMapping(value = "/save")
	public Result<?> save(@RequestBody WzType wzType) {
		if(wzType==null) {
			return Result.error("物资类型信息为空");
		}
			
		if(StringUtils.isEmpty(wzType.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWzTypeService, WzType.class, new String[] {"typeNo","typeName"}, "keyId", wzType, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("物资类型编号和名称不能重复");
			}
			r = setLevel(wzType);
			if(r==null || !r.isSuccess()) {
				return r;
			}
			if(StringUtils.isEmpty(wzType.getTypeOrder())) {
				wzType.setTypeOrder(1);
			}
			wzType.setIsUse(WzConstants.是.getCode());
			iWzTypeService.save(wzType);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWzTypeService, WzType.class, new String[] {"typeNo","typeName"}, "keyId", null, wzType, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("物资类型编号和名称不能重复");
			}
			r = setLevel(wzType);
			if(r==null || !r.isSuccess()) {
				return r;
			}
			iWzTypeService.updateById(wzType);
		}
		wzCleanCache.cleanWzTypeCahce();
		return Result.ok();
	}
	
	
	
	/**
	 * 设置层级
	 * @param sysOrg
	 * @return
	 */
	private Result<?> setLevel(WzType WzType) {
		if(StringUtils.isEmpty(WzType.getParentNo())) {
			WzType.setTypeLevel("["+WzType.getTypeNo()+"]");
		}else if(WzType.getParentNo().equals(WzType.getTypeNo())){
			return Result.error("选择的父类型不能为自己");
		}else{
			WzType parentType = iWzTypeService.queryWzTypeByNo(WzType.getParentNo());
			if(parentType==null) {
				return Result.error("选择的父类型已经不存在");
			}else if(parentType.getTypeLevel().indexOf("["+WzType.getTypeNo()+"]")>-1) {
				return Result.error("选择的父类型不符合要求，存在相互依赖关系");
			}
			WzType.setTypeLevel(parentType.getTypeLevel()+"["+WzType.getTypeNo()+"]");

		}
		return Result.ok();
	}
}

